<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Plugin Plugin
*
* Install this file as application/plugins/plugin_pi.php
*
* @version:     0.1
* $copyright     Copyright (c) Wiredesignz 2009-03-24
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Plugin extends CH_Controller
{

	function __construct() {
		$this->_assign_libraries();
	}
	
	/*function Plugin() {
        $this->_assign_libraries();
    }*/

	function run($data = array()){
        //require_once convert_path(PLUGIN_ROOT.'/'.$data['module_directory'].'/controllers/'.$data['controller'].EXT);
		require_once convert_path('/plugin/'.$data['module_directory'].'/controllers/'.$data['controller'].EXT);
		//$module =& new $data['controller']();
		$module = $data['controller'];

		$method = 'run';
		if (isset($data['method']) && strcmp($method, $data['method'])) {
			$methods = get_class_methods($module); // method_exists function is not case-sensitive
			if (in_array($data['method'], $methods, true)) {
				call_user_func_array(array(&$module, $method), array($data)); // first. $module->run($data) execute
				$method = $data['method'];
			}
		}
		return call_user_func_array(array(&$module, $method), array($data));
    }

    public function render($data = array()) {
        extract($data); //$data['name'] => $name 형으로 변경 
        ob_start();
			//include PLUGIN_ROOT.'/'.$data['module_directory'].'/views/'.$data['skin'].'/'.$data['view'].EXT;
        	include convert_path('/plugin/'.$data['module_directory'].'/views/'.$data['skin'].'/'.$data['view'].EXT);
			$return = ob_get_contents();
		ob_end_clean();
		return $return;
    }
    

	function admin_render($data = array()) {
        extract($data); //$data['name'] => $name 형으로 변경 
        ob_start();
			include PLUGIN_ROOT.'/'.$data['module_directory'].'/views/'.$data['view'].EXT;
			$return = ob_get_contents();
		ob_end_clean();
		return $return;
    }

    function load($object) {
        $this->$object = load_class(ucfirst($object));
    }

    function _assign_libraries() {
    	$CI = get_instance();
    	foreach (get_object_vars($CI) as $key => $object) {
    		$this->$key = $CI->$key;
    	}
    }
}